package com.asiainfo_linkage.service;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.impl.SimpleDataSource;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.service.EntityService;

import com.asiainfo_linkage.model.AddMember;
import com.asiainfo_linkage.model.RemPlan;
import com.asiainfo_linkage.model.RemTask;

@IocBean
public class SendService extends EntityService<RemTask>{
	@Inject
	protected Dao dao;

	//获取当前日期
	SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
	
//	String today=sdf.format(new Date());
	
	Date today=new Date();
	
	private SimpleDataSource dataSource; 
	
	public SimpleDataSource getDataSource(){
		if(dataSource == null){
			dataSource = new SimpleDataSource(); 
			try {
				dataSource.setDriverClassName("org.h2.Driver");
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			dataSource.setJdbcUrl("jdbc:h2:~/test"); 
			dataSource.setUsername("sa"); 
			dataSource.setPassword(""); 
		}
		return dataSource;
	}
	
	public void closeDataSource(){
		try {
			getDataSource().getConnection().close();
			System.out.println("关闭数据源");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

 //查询执行日期为今天的有效任务
   public List<RemTask> queryTaskAll(){
	   	Criteria cri = Cnd.cri();
		cri.where().and("rem_status","=","1");
		Dao dao = new NutDao(getDataSource());
		List<RemTask> list=dao.query(RemTask.class, cri, null);
		//List<RemTask> list = this.dao.query(RemTask.class, Cnd.where("rem_status", "=", "1"), null);
		//查询关联数据。
		/*for(RemTask remTask:list){
			if(remTask.getMembers()==null){
				System.out.println("members为空！");
			}else{
			this.dao().fetchLinks(dao.fetch(RemTask.class,"task_id"), "member_id");
			}
		}*/
		return list;
	}
	//查询指定task_id的计划
	public List<RemPlan> queryPlanALL(RemPlan remPlan,RemTask remTask) {
		// TODO Auto-generated method stub
		Criteria cri = Cnd.cri();
		cri.where().and("task_id","=",remTask.getTask_id());
		Dao dao = new NutDao(getDataSource());
		return dao.query(RemPlan.class, cri, null);
	}
	//修改任务状态为无效
	public void statusUpdate(RemTask remTask){
		Dao dao = new NutDao(getDataSource());
		remTask.setRem_status("0");
		dao.update(remTask);	
	}
	//修改任务的第一次执行时间
	public void firsttimeUpdate(RemTask remTask,Date rem_first_time){
		Dao dao = new NutDao(getDataSource());
		remTask.setRem_first_time(rem_first_time);
		dao.update(remTask);	
	}
	//修改rem_plan表中nexttime
	public void nexttimeUpdate(RemPlan remPlan,Date nexttime){
		Dao dao = new NutDao(getDataSource());
		remPlan.setNexttime(nexttime);
		dao.update(remPlan);	
	}
	public List<AddMember> queryTaskMembers(RemTask remTask){
		Dao dao = new NutDao(getDataSource());
		//RemTask members=dao.fetchLinks(remTask, "member_id");	
		RemTask members=dao.fetchLinks(remTask, "members");	
		//System.out.println(members.getMembers());
		return  members.getMembers();	
	}
}
	
